package com.fy.service.component.config;

import java.util.Date;

import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.mapper.MetaObjectHandler;
import com.fy.framework.component.config.RequestInfoContextHolder;
import com.fy.framework.component.config.UserContextHolder;

@Component
public class FyMetaObjectHandler extends MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        Object createTime = getFieldValByName("createTime", metaObject);
        if (createTime == null) {
            setFieldValByName("createTime", new Date(), metaObject);
        }
        Object createBy = getFieldValByName("createBy", metaObject);
        if (createBy == null) {
            setFieldValByName("createBy", UserContextHolder.getUserId(), metaObject);
        }
        Object createIp = getFieldValByName("createIp", metaObject);
        if (createIp == null) {
            setFieldValByName("createIp", RequestInfoContextHolder.getIp(), metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        setFieldValByName("lastUpdateTime", new Date(), metaObject);
        setFieldValByName("lastUpdateBy", UserContextHolder.getUserId(), metaObject);
        setFieldValByName("lastUpdateIp", RequestInfoContextHolder.getIp(), metaObject);
    }

}
